from django.db import models
from stdimage import StdImageField
from django.utils.safestring import mark_safe
from search.models import CityModel,ProvinceModel,AreaModel
# 不支持上传文件
from ckeditor.fields import RichTextField
# 支持上传文件
from ckeditor_uploader.fields import RichTextUploadingField

# Create your models here.

class Department(models.Model):
    name = models.CharField(max_length=100,unique=True,verbose_name='科室名称') #部门名称

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'Department'
        verbose_name = '科室表'
        verbose_name_plural = verbose_name

class HospitalTitleModel(models.Model):
    name = models.CharField(max_length=100,unique=True,verbose_name='医院等级') #医院等级

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'HospitalTitle'
        verbose_name = '医院等级表'
        verbose_name_plural = verbose_name



class HospitalModel(models.Model):
    name = models.CharField(max_length=100,unique=True,verbose_name='医院名称') #医院名称
    title = models.ForeignKey(HospitalTitleModel,db_constraint=False,on_delete=models.DO_NOTHING,
                              related_name="hospital_title",
                              null=True,blank=True,verbose_name='医院等级') #医院等级
    department = models.ManyToManyField(Department,db_constraint=False,related_name="hospital_department",
                                        blank=True,verbose_name='科室') #科室
    province = models.ForeignKey(ProvinceModel,db_constraint=False,on_delete=models.DO_NOTHING,blank=True,null=True,
                                 related_name="hospital_province",verbose_name='所属省份') #所属省份
    city = models.ForeignKey(CityModel,db_constraint=False,on_delete=models.DO_NOTHING,blank=True,null=True,
                             related_name="hospital_city",verbose_name='所属城市') #所属城市
    area = models.ForeignKey(AreaModel,db_constraint=False,on_delete=models.DO_NOTHING,blank=True,null=True,
                             related_name="hospital_area",verbose_name='所属区域') #所属区域

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'Hospital'
        verbose_name = '医院表'
        verbose_name_plural = verbose_name

    @property
    def get_department(self):
        return [department.name for department in self.department.all()]


class DoctorTitle(models.Model):
    name = models.CharField(max_length=100,unique=True,verbose_name='职称名称') #职称名称

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'DoctorTitle'
        verbose_name = '医生职称表'
        verbose_name_plural = verbose_name

class DoctorGoodAtModel(models.Model):
    name = models.CharField(max_length=100,unique=True,verbose_name='擅长方向') #擅长领域
    department = models.ManyToManyField(Department,db_constraint=False,null=True,blank=True,
                                        related_name="goodat_department",verbose_name='科室')

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'GoodAt'
        verbose_name = '擅长领域表'
        verbose_name_plural = verbose_name

class DoctorLabel(models.Model):
    name = models.CharField(max_length=100,unique=True,verbose_name='医生标签') #医生标签

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'DoctorLabel'
        verbose_name = '医生标签表'
        verbose_name_plural = verbose_name




class DoctorModel(models.Model):
    name = models.CharField(max_length=100,verbose_name='医生姓名') #医生姓名
    avatar = StdImageField(upload_to="doctor/",null=True,blank=True,default='doctor/default.jpg',verbose_name='头像',
                           variations={
                               'thumb_200x200': {'width': 200, 'height': 200},  # 'large': (600, 400),
                               'thumb_100x100': {'width': 100, 'height': 100},  # 'medium': (300, 200),
                               'thumb_50x50': {'width': 50, 'height': 50}  # 'thumbnail': (100, 100),
                           })
    department = models.ForeignKey(Department,on_delete=models.DO_NOTHING,db_constraint=False,
                                   related_name="doctor_department",verbose_name='科室') #科室
    hospital = models.ForeignKey(HospitalModel,on_delete=models.DO_NOTHING,db_constraint=False,
                                 related_name="doctors_hospital",verbose_name='医院') #医院
    title = models.ForeignKey(DoctorTitle,on_delete=models.DO_NOTHING,db_constraint=False,
                              related_name="doctor_title",verbose_name='职称') #
    description = RichTextUploadingField(null=True,blank=True,verbose_name='医生描述') #医生简介
    intro = models.TextField(null=True,blank=True,verbose_name='医生介绍') #医生介绍
    rate = models.DecimalField(max_digits=3,decimal_places=2,null=True,blank=True,verbose_name='评分') #评分
    price = models.IntegerField(default=0,null=True,blank=True,verbose_name='价格') #价格
    reply = models.IntegerField(default=0,null=True,blank=True,verbose_name='回复') #回复
    reply_time = models.IntegerField(default=5,null=True,blank=True,verbose_name='回复时间') #回复时间
    goodat = models.ManyToManyField(DoctorGoodAtModel,verbose_name='擅长领域',blank=True,
                                    db_constraint=False,related_name="doctor_goodat") #擅长领域
    label = models.ManyToManyField(DoctorLabel,verbose_name='医生标签',blank=True,db_constraint=False,
                                   related_name="doctor_label") #医生标签



    def __str__(self):
        return self.name

    class Meta:
        db_table = 'Doctor'
        verbose_name = '医生表'
        verbose_name_plural = verbose_name

    @property
    def get_goodat(self):
        return [goodat.name for goodat in self.goodat.all()]

    @property
    def get_label(self):
        return [label.name for label in self.label.all()]

    @property
    def avatar_display(self):
        return "http://127.0.0.1:8000{}".format(self.avatar.thumb_100x100.url)

    def avatar_small(self):
        return mark_safe(f'<img style="border-radius:100%" alt="" src="{self.avatar.thumb_50x50.url}"></img>')

    # 图片的描述
    avatar_small.short_description = '医生图片'  # 定义了admin后台中该字段的简短描述，即“用户图片”
    # 图片是否显示
    avatar_small.allow_tags = True  # 定义了admin后台中该字段是否显示为html标签，即图片显示
    # 设置排序顺序
    avatar_small.admin_order_field = "avatar"  # 定义了admin后台中该字段的排序方式，即按avatar排序





